<template>
  <el-dialog
    :title="!dataForm.bargainId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="商品id" prop="goodsId">
      <el-input v-model="dataForm.goodsId" placeholder="商品id"></el-input>
    </el-form-item>
    <el-form-item label="商品名称" prop="goodsName">
      <el-input v-model="dataForm.goodsName" placeholder="商品名称"></el-input>
    </el-form-item>
    <el-form-item label="原价" prop="goodsPriced">
      <el-input v-model="dataForm.goodsPriced" placeholder="原价"></el-input>
    </el-form-item>
    <el-form-item label="低价" prop="goodsPrice">
      <el-input v-model="dataForm.goodsPrice" placeholder="低价"></el-input>
    </el-form-item>
    <el-form-item label="开始时间" prop="startDate">
      <el-input v-model="dataForm.startDate" placeholder="开始时间"></el-input>
    </el-form-item>
    <el-form-item label="结束时间" prop="endDate">
      <el-input v-model="dataForm.endDate" placeholder="结束时间"></el-input>
    </el-form-item>
    <el-form-item label="砍价规则" prop="ruleNum">
      <el-input v-model="dataForm.ruleNum" placeholder="砍价规则"></el-input>
    </el-form-item>
    <el-form-item label="砍价规则标识" prop="ruleFlag">
      <el-input v-model="dataForm.ruleFlag" placeholder="砍价规则标识"></el-input>
    </el-form-item>
    <el-form-item label="活动描述" prop="activityDesc">
      <el-input v-model="dataForm.activityDesc" placeholder="活动描述"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          bargainId: 0,
          goodsId: '',
          goodsName: '',
          goodsPriced: '',
          goodsPrice: '',
          startDate: '',
          endDate: '',
          ruleNum: '',
          ruleFlag: '',
          activityDesc: ''
        },
        dataRule: {
          goodsId: [
            { required: true, message: '商品id不能为空', trigger: 'blur' }
          ],
          goodsName: [
            { required: true, message: '商品名称不能为空', trigger: 'blur' }
          ],
          goodsPriced: [
            { required: true, message: '原价不能为空', trigger: 'blur' }
          ],
          goodsPrice: [
            { required: true, message: '低价不能为空', trigger: 'blur' }
          ],
          startDate: [
            { required: true, message: '开始时间不能为空', trigger: 'blur' }
          ],
          endDate: [
            { required: true, message: '结束时间不能为空', trigger: 'blur' }
          ],
          ruleNum: [
            { required: true, message: '砍价规则不能为空', trigger: 'blur' }
          ],
          ruleFlag: [
            { required: true, message: '砍价规则标识不能为空', trigger: 'blur' }
          ],
          activityDesc: [
            { required: true, message: '活动描述不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.bargainId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.bargainId) {
            this.$http({
              url: this.$http.adornUrl(`/bargain/bargainactivity/info/${this.dataForm.bargainId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.goodsId = data.bargainActivity.goodsId
                this.dataForm.goodsName = data.bargainActivity.goodsName
                this.dataForm.goodsPriced = data.bargainActivity.goodsPriced
                this.dataForm.goodsPrice = data.bargainActivity.goodsPrice
                this.dataForm.startDate = data.bargainActivity.startDate
                this.dataForm.endDate = data.bargainActivity.endDate
                this.dataForm.ruleNum = data.bargainActivity.ruleNum
                this.dataForm.ruleFlag = data.bargainActivity.ruleFlag
                this.dataForm.activityDesc = data.bargainActivity.activityDesc
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/bargain/bargainactivity/${!this.dataForm.bargainId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'bargainId': this.dataForm.bargainId || undefined,
                'goodsId': this.dataForm.goodsId,
                'goodsName': this.dataForm.goodsName,
                'goodsPriced': this.dataForm.goodsPriced,
                'goodsPrice': this.dataForm.goodsPrice,
                'startDate': this.dataForm.startDate,
                'endDate': this.dataForm.endDate,
                'ruleNum': this.dataForm.ruleNum,
                'ruleFlag': this.dataForm.ruleFlag,
                'activityDesc': this.dataForm.activityDesc
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
